package com.example.carrierapp.ui.main.goodssource.details

import android.content.Context
import com.example.carrierapp.base.BasePresenter
import com.example.carrierapp.bean.goodssource.BidRequestBody
import com.example.carrierapp.config.toast
import com.example.carrierapp.net.ApiMiddleHelper
import com.example.carrierapp.net.listener.DataCallBackListener
import com.example.carrierapp.util.LogUtil
import com.example.carrierapp.util.ToastUtils
import com.example.carrierapp.viewmodel.itemmodel.ItemGoodsSourceModel
import com.example.carrierapp.viewmodel.viewmodel.GoodsSourceDetailsModel
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import java.util.ArrayList
import kotlin.collections.HashMap
import kotlin.collections.set

/**
 * 货源大厅
 */
class GoodsSourceDetailsPresenter(private val context: Context) : BasePresenter<GoodsSourceDetailsContract.View>(), GoodsSourceDetailsContract.Presenter {

    override fun doBid(lines: MutableList<ItemGoodsSourceModel>?) {
        val params = BidRequestBody()
        val input = ArrayList<Map<String, Any>>()
        lines?.forEach {
            val item = HashMap<String, Any>()
            item["lineId"] = it.id
            item["offer"] = it.offer
            input.add(item)
        }
        params.input = input
        ApiMiddleHelper.getInstance().toNetIo2Main(context, apiStrores.doBid(params), object : DataCallBackListener {
            override fun onSuccess(json: String?) {
                json?.let {
                    LogUtil.response("FetchGoodsSourceListSuccess : $it")
                    view.onBidSuccess()
                }
            }

            override fun onFailure(msg: String?) {
                toast(msg)
                LogUtil.v("FetchGoodsSourceListError : $msg")
                ToastUtils.showCenterToast(context, msg)
                view.onBidError()
            }
        })
    }

    override fun fetchGoodsRecordDetails(id: Int, lineId: Int) {
        fetchData(id, lineId, true)
    }

    override fun fetchGoodsSourceDetails(id: Int, lineId: Int) {
        fetchData(id, lineId, false)
    }

    private fun fetchData(id: Int, lineId: Int, isDetails: Boolean) {
        val params = HashMap<String, String>()
        params["id"] = id.toString()
        if (lineId != 0)
            params["lineId"] = lineId.toString()
        ApiMiddleHelper.getInstance().toNetIo2Main(context, if (isDetails) apiStrores.fetchGoodsRecordDetails(params) else apiStrores.fetchGoodsSourceDetails(params), object : DataCallBackListener {
            override fun onSuccess(json: String?) {
                json?.let {
                    LogUtil.response("FetchGoodsSourceListSuccess : $it")
                    view.onFetchSuccess(Gson().fromJson<GoodsSourceDetailsModel>(it, object : TypeToken<GoodsSourceDetailsModel>() {}.type))
                }
            }

            override fun onFailure(msg: String?) {
                LogUtil.v("FetchGoodsSourceListError : $msg")
                view.onFetchError()
            }
        })
    }

    override fun start() {

    }
}
